package com.base.vistter.iframe.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.base.vistter.iframe.entity.InverterStation;
import com.base.vistter.iframe.model.StationResponse;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface InverterStationMapper extends BaseMapper<InverterStation> {

    @Select("SELECT SUM(capacity) as cs,SUM(power) as ps ,SUM(month_electric) as me , SUM(year_electric) as ye, SUM(total_electric) as te FROM inverter_station WHERE is_delete = 0")
    Map<String, Float> getElectric();

    @Select("select * from inverter_station where is_delete = 0 AND sys_user_id = ${userId} ORDER BY create_date asc")
    List<InverterStation> myStation(@Param("userId") Integer userId);

    @Select({
            "<script>",
            "SELECT count(*) FROM inverter_station ins ",
            "        LEFT JOIN ( ",
            "        SELECT ",
            "            inverter_station_id, ",
            "            SUM( p ) AS p, ",
            "            SUM( dp ) AS dp ",
            "        FROM ( ",
            "                SELECT * FROM ",
            "                    (SELECT *, ROW_NUMBER() OVER ( PARTITION BY inverter_devices_id ORDER BY report_time DESC ) AS rn ",
            "                        FROM inverter_devices_power_day ",
            "                        WHERE report_time BETWEEN #{startTime} AND #{endTime} AND is_delete = 0 ",
            "                         <if test='stationIds != null and !stationIds.isEmpty()'> AND inverter_station_id IN ",
            "                               <foreach collection='stationIds' item='id' open='(' separator=',' close=')'>",
            "                                   #{id}",
            "                               </foreach>",
            "                         </if>",
            "                    ) sub ",
            "                WHERE rn = 1 ",
            "            ) station_power_day ",
            "        GROUP BY inverter_station_id ",
            "    ) power_day ON power_day.inverter_station_id = ins.id ",
            "WHERE ins.is_delete = 0 ",
            " <if test='name != null and name != \"\"'> AND ins.name like CONCAT('%', #{name}, '%') </if>",
            " <if test='status != null'> AND ins.status = #{status} </if>",
            " <if test='stationIds != null and !stationIds.isEmpty()'> AND ins.id IN ",
            "   <foreach collection='stationIds' item='id' open='(' separator=',' close=')'>",
            "       #{id}",
            "   </foreach>",
            "</if>",
            "</script>"})
    long pageStationAppCount(@Param("userId") Integer userId, @Param("name") String name, @Param("status") Integer status, @Param("startTime") Date startTime, @Param("endTime") Date endTime, @Param("stationIds") List<Integer> stationIds);

    @Select({
            "<script>",
            "SELECT ins.id, ins.name, ins.capacity, ",
            "    COALESCE (power_day.dp, 0) / ins.capacity AS equivalentTime, ",
            "    ins.month_electric + COALESCE ( power_day.dp, 0) AS monthElectric, ",
            "    ins.year_electric + COALESCE ( power_day.dp, 0) AS yearElectric, ",
            "    ins.total_electric + COALESCE ( power_day.dp, 0) AS totalElectric, ",
            "    CASE ",
            "        WHEN ic.id IS NOT NULL THEN ",
            "            1 ELSE 0 ",
            "        END AS is_collected, ",
            "    COALESCE (power_day.p, 0) as p, ",
            "    COALESCE (power_day.dp, 0) as dp, ",
            "    ic.create_date ",
            "FROM inverter_station ins ",
            "        LEFT JOIN inverter_collect ic ON ic.inverter_station_id = ins.id ",
            "        AND ic.sys_user_id = #{userId} AND ic.is_delete = 0 ",
            "        LEFT JOIN ( ",
            "        SELECT inverter_station_id, SUM( p ) AS p, SUM( dp ) AS dp ",
            "        FROM ",
            "            ( SELECT * FROM ",
            "                    ( SELECT *, ROW_NUMBER() OVER ( PARTITION BY inverter_devices_id ORDER BY report_time DESC ) AS rn ",
            "                        FROM inverter_devices_power_day ",
            "                        WHERE report_time BETWEEN #{startTime} AND #{endTime} AND is_delete = 0 ",
            "                         <if test='stationIds != null and !stationIds.isEmpty()'> AND inverter_station_id IN ",
            "                               <foreach collection='stationIds' item='id' open='(' separator=',' close=')'>",
            "                                   #{id}",
            "                               </foreach>",
            "                         </if>",
            "                    ) sub ",
            "                WHERE rn = 1 ",
            "            ) station_power_day ",
            "        GROUP BY inverter_station_id ",
            "    ) power_day ON power_day.inverter_station_id = ins.id ",
            "WHERE ins.is_delete = 0 ",
            "<if test='name != null and name != \"\"'> AND ins.name like CONCAT('%', #{name}, '%') </if>",
            "<if test='status != null'> AND ins.status = #{status} </if>",
            "<if test='stationIds != null and !stationIds.isEmpty()'> AND ins.id IN ",
            "   <foreach collection='stationIds' item='id' open='(' separator=',' close=')'>",
            "       #{id}",
            "   </foreach>",
            "</if>",
            "<if test='field != null and field != \"\"'> ORDER BY ${field} ${sort} </if>",
            "limit #{start},#{limit}",
            "</script>"})
    List<StationResponse> pageStationAppList(@Param("start") long start, @Param("limit") long limit, @Param("userId") Integer userId, @Param("name") String name, @Param("status") Integer status, @Param("startTime") Date startTime, @Param("endTime") Date endTime, @Param("stationIds") List<Integer> stationIds, @Param("field") String field, @Param("sort") String sort);

}
